home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / spheremap / GLSMAP / RTSMAP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  11.2 KB  |  481 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1998.  */
  3.  
  4. /* This program is freely distributable without licensing fees
  5.    and is provided without guarantee or warrantee expressed or
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. /* rtsmap.c - real-time generation and use of a sphere map with libglsmap */
  9.  
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13. #include <GL/glut.h>
  14. #include <GL/glsmap.h>
  15.  
  16. #if defined(GL_EXT_texture_object) && !defined(GL_VERSION_1_1)
  17. #define glBindTexture(A,B)     glBindTextureEXT(A,B)
  18. #endif
  19.  
  20. enum {
  21.     TO_NONE = 0,
  22.     TO_FRONT, TO_TOP, TO_BOTTOM, TO_LEFT, TO_RIGHT,
  23.     TO_BACK,
  24.     TO_SPHERE_MAP
  25. };
  26.  
  27. static GLuint texobjs[6] = {
  28.     TO_FRONT, TO_TOP, TO_BOTTOM,
  29.     TO_LEFT, TO_RIGHT, TO_BACK
  30. };
  31.  
  32. SphereMap *smap;
  33.  
  34. GLfloat up[3] = { 0, 1, 0 };
  35. GLfloat eye[3] = { 0, 0, -20 };
  36. GLfloat obj[3] = { 0, 0, 0 };
  37.  
  38. int W, H;
  39.  
  40. int showSphereMap = 0;
  41. int object = 0;
  42. int texdim = 64;
  43. int doubleBuffer = 1;
  44. int dynamicSmap = 1;
  45. int cullBackFaces = 1;
  46. int doSphereMap = 1;
  47. int multipass = 0;
  48.  
  49. static char *pattern[] = {
  50.   "......xxxx......",
  51.   "......xxxx......",
  52.   "......xxxx......",
  53.   "......xxxx......",
  54.   "......xxxx......",
  55.   "......xxxx......",
  56.   "xxxxxxxxxxxxxxxx",
  57.   "xxxxxxxxxxxxxxxx",
  58.   "xxxxxxxxxxxxxxxx",
  59.   "xxxxxxxxxxxxxxxx",
  60.   "......xxxx......",
  61.   "......xxxx......",
  62.   "......xxxx......",
  63.   "......xxxx......",
  64.   "......xxxx......",
  65.   "......xxxx......",
  66. };
  67.  
  68. static void
  69. makePatternTexture(void)
  70. {
  71.   GLubyte patternTexture[16][16][4];
  72.   GLubyte *loc;
  73.   int s, t;
  74.  
  75.   /* Setup RGB image for the texture. */
  76.   loc = (GLubyte*) patternTexture;
  77.   for (t = 0; t < 16; t++) {
  78.     for (s = 0; s < 16; s++) {
  79.       if (pattern[t][s] == 'x') {
  80.     /* Nice green. */
  81.         loc[0] = 0x1f;
  82.         loc[1] = 0x8f;
  83.         loc[2] = 0x1f;
  84.         loc[3] = 0x7f;  /* opaque */
  85.       } else {
  86.     /* Light gray. */
  87.         loc[0] = 0x00;
  88.         loc[1] = 0x00;
  89.         loc[2] = 0x00;
  90.         loc[3] = 0x00;  /* transparent */
  91.       }
  92.       loc += 4;
  93.     }
  94.   }
  95.  
  96.   glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  97.  
  98.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  99.     GL_LINEAR_MIPMAP_LINEAR);
  100.   gluBuild2DMipmaps(GL_TEXTURE_2D, 4, 16, 16,
  101.     GL_RGBA, GL_UNSIGNED_BYTE, patternTexture);
  102. }
  103.  
  104. void
  105. reshape(int w, int h)
  106. {
  107.     W = w;
  108.     H = h;
  109. }
  110.  
  111. void
  112. positionLights(int view, void *context)
  113. {
  114.     static GLfloat light1Pos[4] = { -41.0, 41.0, -41.0, 1.0 };
  115.     static GLfloat light2Pos[4] = { +41.0, 0.0, -41.0, 1.0 };
  116.     static GLfloat light3Pos[4] = { -41.0, 0.0, +41.0, 1.0 };
  117.     static GLfloat light4Pos[4] = { +41.0, 0.0, +41.0, 1.0 };
  118.  
  119.     glLightfv(GL_LIGHT0, GL_POSITION, light1Pos);
  120.     glLightfv(GL_LIGHT1, GL_POSITION, light2Pos);
  121.     glLightfv(GL_LIGHT2, GL_POSITION, light3Pos);
  122.     glLightfv(GL_LIGHT3, GL_POSITION, light4Pos);
  123. }
  124.  
  125. void
  126. drawView(int view, void *context)
  127. {
  128.     /* right green small cube (+5,0,-8) */
  129.     glPushMatrix();
  130.       glTranslatef(5.0, 0.0, -8.0);
  131.       glRotatef(-45, 1.0, 0.0, 1.0);
  132.       glColor3f(0.0, 1.0, 0.0);
  133.       glutSolidCube(2.0);
  134.     glPopMatrix();
  135.     /* left red cube (-5,0,-8) */
  136.     glPushMatrix();
  137.       glTranslatef(-5.0, 0.0, -8.0);
  138.       glRotatef(45, 1.0, 0.0, 1.0);
  139.       glColor3f(1.0, 0.0, 0.0);
  140.       glutSolidCube(6.0);
  141.     glPopMatrix();
  142.     /* left blue cube (-7,0,0); */
  143.     glPushMatrix();
  144.       glTranslatef(-7.0, 0.0, 0.0);
  145.       glColor3f(0.0, 0.0, 1.0);
  146.       glutSolidCube(5.0);
  147.     glPopMatrix();
  148.     /* right cyan big cube (+7,0,0) */
  149.     glPushMatrix();
  150.       glTranslatef(7.0, 0.0, 0.0);
  151.       glRotatef(30, 1.0, 1.0, 0.0);
  152.       glColor3f(0.0, 1.0, 1.0);
  153.       glutSolidCube(5.0);
  154.     glPopMatrix();
  155.     /* distant yellow sphere (0,0,+10) */
  156.     glPushMatrix();
  157.       glTranslatef(0.0, 0.0, 10.0);
  158.       glColor3f(1.0, 1.0, 0.0);
  159.       glutSolidSphere(7.0, 8, 8);
  160.     glPopMatrix();
  161.     /* top white sphere (0,+5,0) */
  162.     glPushMatrix();
  163.       glTranslatef(0.0, 5.0, 0.0);
  164.       glColor3f(1.0, 1.0, 1.0);
  165.       glutSolidSphere(2.0, 8, 8);
  166.     glPopMatrix();
  167.     /* bottom magenta sphere (0,-7,0) */
  168.     glPushMatrix();
  169.       glTranslatef(0.0, -7.0, 0.0);
  170.       glColor3f(1.0, 0.0, 1.0);
  171.       glutSolidSphere(3.0, 8, 8);
  172.     glPopMatrix();
  173. }
  174.  
  175. void
  176. drawObject(void *context)
  177. {
  178.         static GLfloat xplane[4] = { 1, 0, 0, 0 };
  179.         static GLfloat zplane[4] = { 0, 1, 0, 0 };
  180.  
  181.         if (!cullBackFaces) {
  182.                 glEnable(GL_CULL_FACE);
  183.                 glCullFace(GL_FRONT);
  184.         }
  185.  
  186.     glPushMatrix();
  187.                 glTranslatef(obj[0], obj[1], obj[2]);
  188.                 if (doSphereMap) {
  189.                 glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  190.                 glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_SPHERE_MAP);
  191.                 glEnable(GL_TEXTURE_GEN_S);
  192.                 glEnable(GL_TEXTURE_GEN_T);
  193.                 glEnable(GL_TEXTURE_2D);
  194.                 glBindTexture(GL_TEXTURE_2D, 7);
  195.                 glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
  196.             GL_REPLACE);
  197.                 } else {
  198.                         glTexGenfv(GL_S, GL_OBJECT_PLANE, xplane);
  199.                         glTexGenfv(GL_T, GL_OBJECT_PLANE, zplane);
  200.                         glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  201.                         glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR);
  202.                         glEnable(GL_TEXTURE_GEN_S);
  203.                 glEnable(GL_TEXTURE_GEN_T);
  204.                 glEnable(GL_TEXTURE_2D);
  205.                         glBindTexture(GL_TEXTURE_2D, 100);
  206.                         glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,
  207.             GL_REPLACE);
  208.                 }
  209.  
  210.         glColor3f(1.0, 1.0, 1.0);
  211.         switch (object) {
  212.         case 0:
  213.             glutSolidSphere(3.0, 20, 20);
  214.             break;
  215.         case 1:
  216.             glScalef(3.0, 3.0, 3.0);
  217.             glRotatef(30.0, 1.0, 1.0, 0.0);
  218.             glutSolidIcosahedron();
  219.             break;
  220.         case 2:
  221.             glFrontFace(GL_CW);
  222.             glutSolidTeapot(3.0);
  223.             glFrontFace(GL_CCW);
  224.             break;
  225.         }
  226.  
  227.             glDisable(GL_TEXTURE_GEN_S);
  228.                glDisable(GL_TEXTURE_GEN_T);
  229.     glPopMatrix();
  230.  
  231.         if (!cullBackFaces) {
  232.                 glCullFace(GL_BACK);
  233.         }
  234. }
  235.  
  236. void
  237. display(void)
  238. {
  239.     glDisable(GL_TEXTURE_2D);
  240.     glEnable(GL_DEPTH_TEST);
  241.            glEnable(GL_CULL_FACE);
  242.  
  243.         if (dynamicSmap) {
  244.       smapGenSphereMap(smap);
  245.         }
  246.  
  247.     /* smapGenSphereMap leaves scissor enabled, disable it. */
  248.     glDisable(GL_SCISSOR_TEST);
  249.  
  250.     glViewport(0, 0, W, H);
  251.  
  252.     if (showSphereMap) {
  253.         glClear(GL_COLOR_BUFFER_BIT);
  254.  
  255.         glMatrixMode(GL_PROJECTION);
  256.         glLoadIdentity();
  257.         gluOrtho2D(0, 1, 0, 1);
  258.         glMatrixMode(GL_MODELVIEW);
  259.         glLoadIdentity();
  260.  
  261.         glEnable(GL_TEXTURE_2D);
  262.         glDisable(GL_DEPTH_TEST);
  263.         glBindTexture(GL_TEXTURE_2D, 7);
  264.         glBegin(GL_QUADS);
  265.             glTexCoord2f(0,0);
  266.             glVertex2f(0,0);
  267.             glTexCoord2f(1,0);
  268.             glVertex2f(1,0);
  269.             glTexCoord2f(1,1);
  270.             glVertex2f(1,1);
  271.             glTexCoord2f(0,1);
  272.             glVertex2f(0,1);
  273.         glEnd();
  274.     } else {
  275.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  276.  
  277.         glDisable(GL_TEXTURE_2D);
  278.         glEnable(GL_DEPTH_TEST);
  279.  
  280.         glMatrixMode(GL_PROJECTION);
  281.         glLoadIdentity();
  282.         gluPerspective(60.0, 1.0, 0.5, 40.0);
  283.  
  284.         glMatrixMode(GL_MODELVIEW);
  285.         glLoadIdentity();
  286.         gluLookAt(eye[0], eye[1], eye[2],  /* eye at eye */
  287.                   obj[0], obj[1], obj[2],  /* looking at object */
  288.                   up[0], up[1], up[2]);
  289.  
  290.         positionLights(-1, NULL);
  291.         drawView(-1, NULL);
  292.                 if (multipass) {
  293.                   doSphereMap = 1;
  294.                 }
  295.         drawObject(NULL);
  296.                 if (multipass) {
  297.                   doSphereMap = 0;
  298.                   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  299.                   glDepthFunc(GL_EQUAL);
  300.                   glEnable(GL_BLEND);
  301.           drawObject(NULL);
  302.                   glDisable(GL_BLEND);
  303.                   glDepthFunc(GL_LESS);
  304.                 }
  305.     }
  306.  
  307.     if (doubleBuffer) {
  308.         glutSwapBuffers();
  309.     }
  310. }
  311.  
  312. int angle = 0;
  313. int downx, downy;
  314.  
  315. void
  316. motion(int x, int y)
  317. {
  318.     angle += (x - downx);
  319.     angle = angle % 360;
  320.     eye[0] = sin(angle * 3.14159/180.0) * -20;
  321.     eye[2] = cos(angle * 3.14159/180.0) * -20;
  322.     eye[1] = eye[1] + 0.1 * (y - downy);
  323.     if (eye[1] > +25) eye[1] = +25;
  324.     if (eye[1] < -25) eye[1] = -25;
  325.     smapSetEyeVector(smap, eye);
  326.     glutPostRedisplay();
  327.     downx = x;
  328.     downy = y;
  329. }
  330.  
  331. void
  332. mouse(int button, int state, int x, int y)
  333. {
  334.     if (button == GLUT_LEFT_BUTTON) {
  335.         if (state == GLUT_DOWN) {
  336.             downx = x;
  337.             downy = y;
  338.         }
  339.     }
  340. }
  341.  
  342. void
  343. menu(int value)
  344. {
  345.     switch (value) {
  346.     case 0:
  347.         showSphereMap = 0;
  348.         break;
  349.     case 1:
  350.         showSphereMap = 1;
  351.         break;
  352.     case 4:
  353.         object = (object + 1) % 3;
  354.         break;
  355.     case 5:
  356.         smapSetSphereMapTexDim(smap, 64);
  357.         smapSetViewTexDim(smap, 64);
  358.         break;
  359.     case 6:
  360.         smapSetSphereMapTexDim(smap, 128);
  361.         smapSetViewTexDim(smap, 128);
  362.         break;
  363.     case 7:
  364.         smapSetSphereMapTexDim(smap, 256);
  365.         smapSetViewTexDim(smap, 256);
  366.         break;
  367.     case 8:
  368.         glDrawBuffer(GL_FRONT);
  369.         glReadBuffer(GL_FRONT);
  370.         doubleBuffer = 0;
  371.         break;
  372.     case 9:
  373.         glDrawBuffer(GL_BACK);
  374.         glReadBuffer(GL_BACK);
  375.         doubleBuffer = 1;
  376.         break;
  377.         case 10:
  378.                 dynamicSmap = 1;
  379.                 break;
  380.         case 11:
  381.                 dynamicSmap = 0;
  382.                 break;
  383.         case 12:
  384.                 cullBackFaces = 1;
  385.                 break;
  386.         case 13:
  387.                 cullBackFaces = 0;
  388.                 break;
  389.         case 14:
  390.                 doSphereMap = 1;
  391.                 multipass = 0;
  392.                 break;
  393.         case 15:
  394.                 doSphereMap = 0;
  395.                 multipass = 0;
  396.                 break;
  397.         case 16:
  398.                 multipass = 1;
  399.                 break;
  400.     case 666:
  401.         exit(0);
  402.         break;
  403.     }
  404.     glutPostRedisplay();
  405. }
  406.  
  407. void
  408. initGraphicsState(void)
  409. {
  410.     GLfloat lightColor[4] = { 0.6, 0.6, 0.6, 1.0 };  /* white */
  411.  
  412.     glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, 1);
  413.     glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor);
  414.     glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor);
  415.     glLightfv(GL_LIGHT2, GL_DIFFUSE, lightColor);
  416.     glLightfv(GL_LIGHT3, GL_DIFFUSE, lightColor);
  417.     glEnable(GL_LIGHTING);
  418.     glEnable(GL_LIGHT0);
  419.     glEnable(GL_LIGHT1);
  420.     glEnable(GL_LIGHT2);
  421.     glEnable(GL_LIGHT3);
  422.     glEnable(GL_DEPTH_TEST);
  423.     glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
  424.     glEnable(GL_COLOR_MATERIAL);
  425.     glEnable(GL_NORMALIZE);
  426. }
  427.  
  428. int
  429. main(int argc, char **argv)
  430. {
  431.     glutInitWindowSize(400, 400);
  432.     glutInit(&argc, argv);
  433.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
  434.     glutCreateWindow("real-time dynamic sphere mapping");
  435.     glutReshapeFunc(reshape);
  436.     glutDisplayFunc(display);
  437.  
  438.     initGraphicsState();
  439.  
  440.     glutCreateMenu(menu);
  441.     glutAddMenuEntry("eye view", 0);
  442.     glutAddMenuEntry("show sphere map", 1);
  443.     glutAddMenuEntry("outline", 2);
  444.     glutAddMenuEntry("switch object", 4);
  445.     glutAddMenuEntry("texdim = 64", 5);
  446.     glutAddMenuEntry("texdim = 128", 6);
  447.     glutAddMenuEntry("texdim = 256", 7);
  448.     glutAddMenuEntry("single buffer", 8);
  449.     glutAddMenuEntry("double buffer", 9);
  450.         glutAddMenuEntry("dynamic smap", 10);
  451.         glutAddMenuEntry("stop smap building", 11);
  452.         glutAddMenuEntry("cull back faces", 12);
  453.         glutAddMenuEntry("cull front faces", 13);
  454.         glutAddMenuEntry("sphere map", 14);
  455.         glutAddMenuEntry("texture", 15);
  456.         glutAddMenuEntry("multipass", 16);
  457.     glutAddMenuEntry("quit", 666);
  458.     glutAttachMenu(GLUT_RIGHT_BUTTON);
  459.  
  460.     glutMouseFunc(mouse);
  461.     glutMotionFunc(motion);
  462.  
  463.     smap = smapCreateSphereMap(NULL);
  464.     smapSetSphereMapTexObj    (smap, TO_SPHERE_MAP);
  465.     smapSetViewTexObjs        (smap, texobjs);
  466.     smapSetEyeVector          (smap, eye);
  467.     smapSetUpVector           (smap, up);
  468.     smapSetObjectVector       (smap, obj);
  469.     smapSetNearFar            (smap, 0.5, 40.0);
  470.     smapSetPositionLightsFunc (smap, positionLights);
  471.     smapSetDrawViewFunc       (smap, drawView);
  472.     smapSetViewTexDim         (smap, 64);
  473.     smapSetSphereMapTexDim    (smap, 64);
  474.  
  475.         glBindTexture(GL_TEXTURE_2D, 100);
  476.         makePatternTexture();
  477.  
  478.     glutMainLoop();
  479.     return 0;
  480. }
  481.